package com.LinkedList.learnLinkedList;

import com.sh.Iterator;

public class LinkedList implements List {



    transient int size = 0;   // 有几个节点

    transient Node first;   // 第一个节点

    transient Node  last;  // 最后一个节点


    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int index) {
        Node node = node(index);
        return node.item;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public void add(int index, Object element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

    @Override
    public void add(Object o) {
        linkLast(o);
        return;
    }

    @Override
    public void addBefore(Object obj, Object e) {

    }

    @Override
    public void addAfter(Object obj, Object e) {

    }

    @Override
    public boolean remove(Object e) {
        return false;
    }

    @Override
    public Object remove(int index) {
        return null;
    }

    @Override
    public Object replace(int i, Object e) {
        return null;
    }

    @Override
    public Iterator interator() {
        return null;
    }

    /**
     * 静态内部类  代表LinkedList的每个节点
     */
    private static class Node<E> {
        E item;   // 节点数据
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "item=" + item +
                    ", next=" + next +
                    ", prev=" + prev +
                    '}';
        }
    }



    void linkLast(Object e) {
        final Node l = last;
        final Node newNode = new Node(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        // modCount++;
    }


    void linkBefore(Object e, Node succ) {
        // assert succ != null;
        final Node pred = succ.prev;
        final Node newNode = new Node(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
//        modCount++;
    }

    /**
     * 返回指定索引位置的节点
     * @param index
     * @return
     */
    Node node(int index) {
        // assert isElementIndex(index);

        // size >> 1   相当于size/2   前一半节点
        if (index < (size >> 1)) {
            Node x = first;
            for (int i = 0; i < index; i++)   // 从第一个节点开始查找
                x = x.next;
            return x;
        } else {   // 后一半节点
            Node x = last;
            for (int i = size - 1; i > index; i--)    // 从最后一个节点开始查找
                x = x.prev;
            return x;
        }
    }

    /**
     * 判断索引是否在合法范围内
     * @param index
     */
    private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    /**
     *  判断索引是否在合法范围内
     */
    private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

    /**
     * 返回拼接越界异常信息
     * @param index
     * @return
     */
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }
}
